package com.rikai.Rikai;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.EditText;

import com.mopub.mobileads.MoPubView;

public class RikaiActivity extends Activity {
    private RikaiWebView rWebView;
    private MoPubView mPubView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        
        // load ads
        mPubView = (MoPubView) findViewById(R.id.adview);
        if (mPubView!=null) {
      	  mPubView.setAdUnitId("agltb3B1Yi1pbmNyDQsSBFNpdGUY-sjfAQw");
      	  mPubView.loadAd();
        }

        rWebView = (RikaiWebView)findViewById(R.id.testrikai);
        String home = rWebView.getLastURL(rWebView.homepage);

        Intent intent = getIntent();
        if (intent != null) {
          Log.e("RIKAI","Intent action:" + intent.getAction());
          if (intent.getAction().equals("android.intent.action.VIEW")) {
            Uri data = intent.getData();
            if (data != null) {
              home = data.toString();
            }
            Log.e("RIKAI","Intent uri:" + data.toString());
          }
        }
        rWebView.loadUrl(home);
    }
    @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.option_menu, menu);
        //optionsMenu = menu;
        return super.onCreateOptionsMenu(menu);
      }
    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.add_bookmark:
            if (rWebView != null) rWebView.rememberLink(rWebView.getLastURL(null));
            return true;
          case R.id.main_menu:
            if (rWebView!=null) rWebView.loadUrl(rWebView.homepage);
            return true;
          case R.id.enter_url:
            if (rWebView!=null) {
              AlertDialog.Builder alert = new AlertDialog.Builder(this);
              alert.setTitle("Go to address");
              alert.setMessage("Please enter a URL");
              final EditText input = new EditText(this);
              input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
              alert.setView(input);

              alert.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  String value = input.getText().toString();
                  if (value.indexOf("://")==-1) value = "http://" + value;
                  if (value.toLowerCase().indexOf("javascript") == 0) {
                    return;
                  }
                  if (value.toLowerCase().indexOf("file") == 0) {
                    return;
                  }
                  rWebView.loadUrl(value);
                }
                });
              alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  // canceled.
                  }});
              alert.show();
            }
            return true;
          default:
            return super.onOptionsItemSelected(item);
        }
      }
    
    @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          if (rWebView.canGoBack()) {
            rWebView.goBack();
            return true;
          } else if (!rWebView.getLastURL(rWebView.homepage).equals(rWebView.homepage)) {
            rWebView.loadUrl(rWebView.homepage);
          }
        }
        return super.onKeyDown(keyCode, event);
      }
}
